package cn.netty.example5.maspack;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;

import java.util.List;

/**
 *
 * MessageToMessageDecoder是netty提供的消息解码器
 *
 *
 * <p>Title: MesgPackDecoder</p>
 * <p>Description:MessagePack的解码器</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2018/10/29
 */
public class MsgpackDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {

        final byte[] array;
        final int length = msg.readableBytes();
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        MessagePack msgPack = new MessagePack();
        out.add(msgPack.read(array));
    }
}
